<template>

  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="(iscollapse)? '64px' : '200px'">
        <div class="collapseBtn" @click="collapseBtnToggle">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition="false"
          :collapse="iscollapse"
          :router="true"
          :default-active="activPath">
          <el-submenu :index="'/'+item1.path" v-for="(item1,index) in menulist" :key="item1.id">
            <template slot="title">
              <i :class="iconClass[index]"></i>
              <span>{{item1.authName}}</span>
            </template>
            <el-menu-item :index="'/'+item2.path" v-for="item2 in item1.children" :key="item2.id" @click="activPathClick('/'+item1.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menulist: [],
      iconClass: ['iconfont icon-user', 'iconfont icon-tijikongjian', 'iconfont icon-shangpin', 'iconfont icon-danju', 'iconfont icon-baobiao'],
      iscollapse: false,
      activPath: ''
    }
  },
  // 调用获取菜单列表函数
  created() {
    this.getMenuList()
    this.activPath = window.sessionStorage.getItem('activPath')
  },
  methods: {
    // 登出
    logout () {
      this.$confirm('是否退出登录', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功',
          showClose: 'true'
        })
        window.sessionStorage.clear()
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消退出',
          showClose: 'true'
        })
      })
    },
    // 获取菜单列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res.data[0])
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    // 侧边栏收起
    collapseBtnToggle() {
      this.iscollapse = !this.iscollapse
    },
    // 二级菜单点击高亮显示
    activPathClick(activPath) {
      window.sessionStorage.setItem('activPath', activPath)
      this.activPath = activPath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  div{
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
  .collapseBtn{
    color: white;
    text-align: center;
    text-space: 0.2em;
    font-size: 10px;
    line-height: 24px;
    background-color: #4a5064;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main{
  background-color: #eaedf1;
}
.home_container{
  height: 100%;
}
.iconfont{
  margin-right: 10px;
}
</style>
